/**
 *******************************************************************************
 * @Brief     : Reset Clock Control(RCC) implementation.
 * @FileName  : tlsr825x_rcc.h
 * @Author    : GaoQiu
 * @CreateDate: 2020-09-10
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *
 *******************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */
#ifndef __TLSR82XX_RCC_H
#define __TLSR82XX_RCC_H

#include "libc/types.h"

/*!   FHS source. bit7 -> 0x66; bit0 -> 0x70 */
#define FHS_SRC_48M             0x00
#define FHS_SRC_24M_RC          0x80
#define FHS_SRC_24M_XTAL        0x01

/*!   System clock source. */
#define SYS_CLK_SRC_24M_RC      (0x00<<5)
#define SYS_CLK_SRC_FHS         (0x01<<5)
#define SYS_CLK_SRC_FHS_DIV     (0x02<<5)
#define SYS_CLK_SRC_32M_XTAL    (0x03<<5)

/*!   32K clock source. */
#define LSC_32K_SRC_RC          0x00
#define LSC_32K_SRC_XTAL        0x01

/*!   DMIC clock source. */
#define DMIC_CLK_SRC_DIV        0x00 //step[6:0]/mode[7:0]
#define DMIC_CLK_SRC_32K        0x02


#define RCC_RESET(x)            (RST->RST##x)
#define RCC_CLOCK_EN(x)         (CLOCK->CLKEN##x)

/**
 * @brief : Get FHS clock rate.
 *
 * @param : none.
 *
 * @return: none.
 */
uint32_t RCC_GetFhsClk(void);

/**
 * @brief : Get system clock rate.
 *
 * @param : none.
 *
 * @return: none.
 */
uint32_t RCC_GetSysClk(void);

/**
 * @brief : Reset specified peripheral.
 *
 * @param : periph    can be RCC_IIC/RCC_SPI ...
 *
 * @return: none.
 */
void RCC_RstPeriph(uint32_t periph);

/**
 * @brief : Reset all peripheral.
 *
 * @param : none.
 *
 * @return: none.
 */
void RCC_RstAllPeriph(void);

/**
 * @brief : Set FHS clock source.
 *
 * @param : fhsSrc    can be FHS_SRC_48M/FHS_SRC_24M_RC...
 *
 * @return: none.
 */
void RCC_SetFhs(uint8_t fhsSrc);

/**
 * @brief : Set system clock source.
 *
 * @param : fhsSrc    can be SYS_CLK_SRC_24M_RC/SYS_CLK_SRC_FHS ...
 * @param : fhsDiv    !!! must > 2
 *                    it will be valid if fhsSrc == SYS_CLK_SRC_FHS_DIV.
 *
 * @return: none.
 */
void RCC_SetSysClk(uint8_t sysSrc, uint8_t fhsDiv);

/**
 * @brief : Enable specified peripheral clock.
 *
 * @param : periph    can be RCC_IIC/RCC_SPI ...
 *
 * @return: none.
 */
void RCC_EnablePeriphClk(uint32_t periph);

/**
 * @brief : Disable specified peripheral clock.
 *
 * @param : periph    can be RCC_IIC/RCC_SPI ...
 *
 * @return: none.
 */
void RCC_DisablePeriphClk(uint32_t periph);

/**
 * @brief : Enable all peripheral clock.
 *
 * @param : none
 *
 * @return: none.
 */
void RCC_EnableAllPeriphClk(void);

/**
 * @brief : Set all peripheral clock to default value.
 *
 * @param : none
 *
 * @return: none.
 */
void RCC_SetAllPeriphClkToDef(void);

/**
 * @brief : define peripheral clock configuration.
 *
 * @param : none
 *
 * @return: none.
 */
void RCC_DefInit(void);

/**
 * @brief : Select 32k clock source for DMIC.
 *
 * @param : src32k     LSC_32K_SRC_RC/LSC_32K_SRC_XTAL
 *
 * @return: none.
 */
void RCC_Set32kClk(uint8_t src32k);

/**
 * @brief : Set DMIC clock.
 *
 * @param : dmicClkSrc     can be DMIC_CLK_SRC_DIV/DMIC_CLK_SRC_32K
 * @param : step
 * @param : mod
 *
 * @return: none.
 */
void RCC_SetDmicClk(uint8_t dmicClkSrc, uint8_t step, uint8_t mod);

/**
 * @brief : Set IIS clock. must set FHS to 48M.
 *
 * @param : step
 * @param : mod
 *
 * @return: none.
 */
void RCC_SetI2sClk(uint8_t step, uint8_t mod);


#endif
